package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.logging.LogManager;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    //分页查询
    @Override
    public Page<Setmeal> findByPage(String name, Integer pageNum, Integer pageSize) {
        //1. 根据name模糊分页查询套餐
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNoneEmpty(name),Setmeal::getName,name);

        //2. 设置分页条件
        Page<Setmeal> page = new Page<>(pageNum, pageSize);

        //3. 通过分页条件获取到套餐，遍历获取某个套餐
        page = setmealMapper.selectPage(page,wrapper);
        List<Setmeal> setmealList = page.getRecords();
        for (Setmeal setmeal : setmealList) {
            //3-1. 根据setmeal的category_id查询category
            Long categoryId = setmeal.getCategoryId();
            Category category = categoryMapper.selectById(categoryId);
            setmeal.setCategoryName(category.getName());
            //3-2. 根据setmeal的id查询setmeal_dish列表
            LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SetmealDish::getSetmealId,setmeal.getId());
            List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);
            setmeal.setSetmealDishes(setmealDishList);
        }
        //4. 组装返回数据
        return page;
    }

    //保存
    @Override
    public void save(Setmeal setmeal) {
        setmealMapper.insert(setmeal);
        //将信息保存到中间表setmealDish中
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
//                LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
//                wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
                setmealDish.setSetmealId(setmeal.getId());//关联id
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    //根据id删除
    @Override
    public void deleteByIds(Long[] ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
        int count = setmealMapper.selectCount(wrapper);
        if (count > 0){
            throw new CustomException("该套餐正在销售中，无法进行删除");
        }
        setmealMapper.deleteBatchIds(Arrays.asList(ids));

        //删除套餐中间表中所有符合条件的数据
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(wrapper1);
    }

    @Override
    public Setmeal selectById(long id) {
        //1. 根据id查询setmeal基本信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //2. 根据查询条件在setmealDish表中查询出信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
        //3. 将setmealDishes信息录入setmeal
        setmeal.setSetmealDishes(setmealDishes);
        //4. 根据查询条件，保存菜品种类
        LambdaQueryWrapper<Category> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Category::getId,setmeal.getCategoryId());
        List<Category> categoryList = categoryMapper.selectList(wrapper1);
        for (Category category : categoryList) {
            setmeal.setCategoryName(category.getName());
            setmeal.setCategoryId(category.getId());
        }
        return setmeal;
    }

    //更新
    @Override
    public void update(Setmeal setmeal) {
        //1.根据条件，先将套餐中的菜品删掉
        Long id = setmeal.getId();
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        setmealDishMapper.delete(wrapper);
        //2. 再将新的信息录入
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //添加setmealId
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        }
        //将修改的菜品列表进行保存
        setmealMapper.updateById(setmeal);
    }

    //停用/启用方法
    @Override
    public void status(Integer status, List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            setmeal.setStatus(status);
            setmealMapper.updateById(setmeal);
        }
    }

    @Override
    public List<Setmeal> findList(long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId);
        wrapper.eq(Setmeal::getStatus,1);
        List<Setmeal> setmealList = setmealMapper.selectList(wrapper);
        return setmealList;
    }
}
